package com.example.java_second_hand_market;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;



public class SellerDialog extends Stage {
    public SellerDialog(Seller by,int width,int height){

        Button exchangeBt = new Button("切换为买家");

        exchangeBt.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Stage stage = (Stage) exchangeBt.getScene().getWindow(); // 获取当前按钮所在的窗口
                stage.close();
                BuyerDialog bd= new BuyerDialog(new Buyer(by.getName(),by.getAccount(),by.getPassword()),width,height);

            }
        });

        TabPane tabPane = new TabPane();

        Stage primaryStage = new Stage();
        // 创建一个 UserInfoDialog 实例
        // 假设有一个 User 类
        AddItemDialog addItemDialog = new AddItemDialog(by,width,height);
        OrderAdminDialog orderAdminDialog = new OrderAdminDialog(by,width,height);
        UserInfoDialog userInfoDialog = new UserInfoDialog(by,width,height);
        ChatDialog chatDialog = new ChatDialog(by,width,height);

        // 创建 Tab，并将 UserInfoDialog 的内容添加到 Tab 中
        Tab addTab = new Tab("添加商品");
        Tab glTab = new Tab("管理订单");
        Tab userinfoTab = new Tab("个人信息");
        Tab chatTab = new Tab("聊天");
        Tab exchangeTab = new Tab("切换为买家");

        addTab.setContent(addItemDialog.getBp());
        addTab.setClosable(false);

        glTab.setContent(orderAdminDialog.getBp());
        glTab.setClosable(false);

        userinfoTab.setContent(userInfoDialog.getVBox());
        userinfoTab.setClosable(false);

        chatTab.setContent(chatDialog.getBp());
        chatTab.setClosable(false);

        HBox hBox = new HBox();
        hBox.getChildren().add(exchangeBt);
        hBox.setAlignment(Pos.CENTER);

        VBox vBox = new VBox();
        vBox.getChildren().add(hBox);
        vBox.setAlignment(Pos.CENTER);
        exchangeTab.setContent(vBox);
        exchangeTab.setClosable(false);

        // 将 Tab 添加到 TabPane 中
        tabPane.getTabs().add(addTab);
        tabPane.getTabs().add(glTab);
        tabPane.getTabs().add(userinfoTab);
        tabPane.getTabs().add(chatTab);
        tabPane.getTabs().add(exchangeTab);
        tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> {
                    if (newTab == glTab) {
                        orderAdminDialog.refresh();
                    }
                });


            // 创建场景并显示

        Scene scene = new Scene(tabPane, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.setTitle("二手市场管理系统");
        primaryStage.show();
    }
};


